package cn.longwi.lwse.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.system.oshi.OshiUtil;
import lombok.extern.slf4j.Slf4j;
import oshi.hardware.NetworkIF;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class HardwareInfoUtil {


    /**
     * 获取计算机硬件信息
     * @return 硬件信息
     */
    public static String getHardwareIdentifier() {
        //hardId 硬件UUID
        String hardwareUUID = "";
        try {
            hardwareUUID = OshiUtil.getSystem().getHardwareUUID();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        List<NetworkIF> networkIFs = OshiUtil.getNetworkIFs();
        String macIp = "";
        if (!CollUtil.isEmpty(networkIFs)){
            List<String> macadamias = networkIFs.stream().map(NetworkIF::getMacaddr).collect(Collectors.toList());
            macIp = StrUtil.join("-",macadamias);
        }
        String id = hardwareUUID;
        if (StrUtil.isNotBlank(macIp)){
            id += macIp;
        }
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        return sha256.digestHex(id);
    }
}
